package Twice.day07.homework;

/**
 * @Author: lya
 * @Description: 本类的作用描述:懒汉模式
 * 在线程并发环境下,会出现
 * @CreateDate: 2019/8/9
 * @Version: 1.0
 */
public class LazyMyDataSource {
    //定义一个本类唯一一个实例
    //static赋值机会只有一次 - jvm加载类的时候
    private static LazyMyDataSource instance = null;
    //私有化构造
    private LazyMyDataSource(){
        //一般是用来进行资源的一些配资
        //需要消耗一定的时间
        System.out.println("实例化...");

    }
    //定义一个公开的静态的方法来返回这个唯一实例
    //synchronized  - 锁 - 整个方法都给锁住
    public static synchronized LazyMyDataSource getInstance(){
        return instance == null ? instance = new LazyMyDataSource():instance;
    }
}
class LazyMyDataSource1{
    private static LazyMyDataSource1 instance = null ;
    private LazyMyDataSource1(){
        System.out.println("1.......");
    }
    public static synchronized LazyMyDataSource1 getInstance(){
        return instance == null ? instance = new LazyMyDataSource1():instance;
    }
}
class LazyMyDataSource2{
    private static LazyMyDataSource2 instance  = null;
    private LazyMyDataSource2(){
        System.out.println("2......");
    }
    public static synchronized LazyMyDataSource2 getInstance(){
        return instance == null? instance = new LazyMyDataSource2():instance ;
    }
}
class LazyMyDataSource3{
    private static LazyMyDataSource3 instance =null;
    private LazyMyDataSource3(){
        System.out.println("3...");
    }
    public static synchronized LazyMyDataSource3 getInstance(){
        return instance == null ? instance = new LazyMyDataSource3():instance;
    }
}
class LazyMyDataSource4{
    private static LazyMyDataSource4 instance = null;
    private LazyMyDataSource4(){
        System.out.println("4..........");
    }
    public static synchronized LazyMyDataSource4 getInstance(){
        return instance == null ? instance = new LazyMyDataSource4():instance;
    }
}
